<template>
	<div id="drag-dialog">
		<template v-if="isScaleMini !=='mini'">
			<el-dialog v-draggable modal :modal-append-to-body="false" v-bind="$attrs" :show-close="showClose" :fullscreen="isFullscreen" :visible.sync="visible"
				@open="handleOperate('open')" @close="handleOperate('close')" :before-close="handleBeforeCLose" ref="dialog" :width="width" top="20px">
				<span slot="title" class="dialog-title">
					<div class="dialog-title">{{header}}</div>
					<div class="dialog-bar">
						<el-tooltip class="item" effect="dark" content="最小化" placement="top-start">
							<i class="el-icon-minus" @click="handleCLick('mini')"></i>
						</el-tooltip>
						<el-tooltip class="item" effect="dark" :content="isFullscreen ? '缩小' : '全屏'" placement="top-start">
							<i class="el-icon-full-screen" @click="handleCLick('fullscreen')"></i>
						</el-tooltip>
						<el-tooltip class="item" effect="dark" content="关闭" placement="top-start">
							<i class="el-icon-close" @click="handleCLick('close')"></i>
						</el-tooltip>
					</div>
				</span>
				<div>
					<component v-bind:is="componentName"></component>
				</div>
				<span slot="footer" class="dialog-footer" v-if="isFooter">
					<el-button type="primary" @click="handleSubmit()">提 交</el-button>
				</span>
			</el-dialog>
		</template>
		<template v-else>
			<article v-show="visible" class="mini-dialog" @click="handleClickMini">
				<slot name="mini-title">
					<el-tooltip :content="minContent" placement="top-start">
						<span class="mini-content">{{minContent}}</span>
					</el-tooltip>
				</slot>
				<div class="mini-bar">
					<el-tooltip class="item" effect="dark" content="还原" placement="top-start">
						<i class="el-icon-rank" @click="handleOpen"></i>
					</el-tooltip>
					<el-tooltip class="item" effect="dark" content="关闭" placement="top-start">
						<i class="el-icon-close" @click="handleClose"></i>
					</el-tooltip>
				</div>
			</article>
		</template>
	</div>
</template>

<script>
export default {
	name: "draggable-dialog",
	props: {
		visible: {
			type: Boolean,
			default: false
		},
		fullscreen: {
			type: Boolean,
			default: false
		},
		showClose: {
			type: Boolean,
			default: false
		},
		header: {
			type: String,
			default: ""
		},
		type: {
			type: String,
			default: "dialog"
		},
		isFooter: {
			type: Boolean,
			default: false
		},
		componentName: {
			type: String,
			default: ""
		},
		width: {
			type: String,
			default: "50%"
		},
		height: {
			type: String,
			default: "50%"
		}
	},
	computed: {
		isScaleMini() {
			return this.type;
		}
	},
	data() {
		return {
			timer: null,
			isFullscreen: this.fullscreen,
			minContent: "最小化内容"
		};
	},
	mounted() {
		this.$nextTick(() => {
			this.setDialogHeight(this.width, this.height, 200);
		});
	},
	methods: {
		handleBeforeCLose(done) {
			this.$emit("update:visible", false);
			this.$emit("before-close");
		},
		handleOpen() {
			this.setDialogHeight(this.width, this.height);
			this.setDialogBodyBottom();
			this.$emit("update:type", "dialog");
		},
		handleClose() {
			this.$emit("update:visible", false);
		},
		// 最小化弹框
		handleClickMini() {
			this.$emit("miniChange");
		},
		handleSubmit() {
			this.$emit("submit", this.data);
		},
		// 弹框事件-打开/关闭
		handleOperate(type) {
			if (type === "open") {
				this.setDialogBodyBottom(200);
			}
			this.$emit(type);
		},
		// 操作事件
		handleCLick(type) {
			switch (type) {
				case "fullscreen":
					this.isFullscreen = !this.isFullscreen;
					this.height = this.isFullscreen ? "100%" : "50%";
					this.width = this.isFullscreen ? "99.1%" : "50%";
					this.setDialogHeight(this.width, this.height);
					this.$emit("update:fullscreen", !this.fullscreen);
					break;
				case "mini":
					this.isScaleMini = type;
					this.$emit("update:type", type);
					break;
				case "close":
					this.$emit("update:visible", false);
					break;
			}
		},
		setDialogHeight(width, height, timeout) {
			this.timer = setTimeout(() => {
				let dialog = this.$refs.dialog.$el.querySelector(".el-dialog");
				dialog.style.width = width;
				dialog.style.height = height;
				dialog.style.marginLeft = this.isFullscreen ? 0 : "25%";
			}, timeout ? timeout : 0);
		},
		setDialogBodyBottom(timeout) {
			this.timer = setTimeout(() => {
				this.$refs.dialog.$el.querySelector(".el-dialog__body").style.bottom = this.isFooter ? "70px" : "10px";
			}, timeout ? timeout : 0);
		}
	},
	beforeDestroy() {
		if (this.timer) {
			clearTimeout(this.timer); // 在Vue实例销毁前，清除我们的定时器
		}
	}
};
</script>

<style scoped>
#drag-dialog >>> .el-dialog {
	min-height: 25%;
	display: flex;
	flex-wrap: wrap;
	flex-direction: column;
	justify-content: space-between;
	background-color: #F5F5F5;
}

#drag-dialog >>> .el-dialog__header {
	padding: 5px 10px;
	background-color: #AFC5E0;
	font-size: 18px;
}

.dialog-title {
	font-size: 18px;
	display: flex;
	justify-content: space-between;
}

#drag-dialog >>> .el-dialog__body {
	position: absolute;
	left: 0;
	top: 50px;
	bottom: 70px;
	right: 0;
	padding: 10px;
	margin-top: 5px;
	z-index: 1;
	overflow: hidden;
	overflow-y: auto;
}

.dialog-footer {
	border-top: 1px solid #EEEEEE;
	padding: 2vh 0;
}

.dialog-bar {
	transform: translate(0vw, 1px);
	display: flex;
	justify-content: flex-end;
	align-items: center;
	width: 3em;
	float: right;
	font-size: 18px;
	color: #9E9E9E;
}

.dialog-bar i {
	margin-left: 15px;
	flex: 1;
}

.dialog-bar i:hover {
	cursor: pointer;
	color: steelblue;
}

.mini-dialog {
	width: 33vw;
	height: 36px;
	background: #607085;
	position: fixed;
	font-size: 16px;
	right: 1vw;
	bottom: 1vh;
	border-radius: 10px;
	padding: 0 20px;
	color: #FFFFFF;
	display: inline-flex;
	justify-content: space-between;
	align-items: center;
	z-index: 9000;
}

.mini-content {
	width: 30vw;
	text-align: left;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}

.mini-bar i {
	margin-left: 8px;
	flex: 1;
}

.mini-bar i:hover {
	cursor: pointer;
	color: steelblue;
}
</style>
